<template>
    <!-- 头部 -->
    <header class="header">
        <!-- 头部的第一行 -->
        <div class="top">
            <div class="container">
                <div class="loginList">
                    <p style="font-size: medium;">11商城欢迎您！</p>
                    <p>
                        <span v-if="judge" style="font-size: medium; padding-right: 5px">{{ nickName }}您好</span>
                        <button v-if="judge" @click="logout" style="font-size: medium;border: none;">退出</button>
                        <router-link v-if="!judge" to="/login" style="font-size: medium;">登录</router-link>
                        <router-link v-if="!judge" to="/register" class="register"
                            style="font-size: medium;">注册</router-link>
                        <router-link v-if="!judge || judge" to="/home" class="home"
                            style="font-size: medium;">首页</router-link>

                    </p>
                </div>
                <div class="typeList">
                    <router-link @click="isLogin" to="/order" style="font-size: medium;">我的订单</router-link>
                    <router-link @click="isLogin" to="/shopcart" style="font-size: medium;">我的购物车</router-link>
                </div>
            </div>
        </div>
    </header>
</template>

<script>
import axios from 'axios'
export default {
    name: 'Header',
    data() {
        return {
            nickName: '用户',
            judge: false
        }
    },
    created: function () {
        this.isLogin();
    },
    beforeUpdate: function () {
        this.isLogin();
    },
    methods: {
        isLogin() {
            const token = localStorage.getItem("token");
            if (token != null) {
                this.judge = true
            } else {
                this.judge = false
                // alert("请登录或注册！！")
                // this.$router.push('/login');
                if (this.$route.path !== '/login' && this.$route.path !== '/register') {
                    this.$router.push('/login');

                }
            }
            // return this.judge;
        },
        goSearch() {
            this.$router.push('/search')
        },
        isLoggedIn() {
            axios({
                headers: {
                    'token': localStorage.getItem('token')
                },
                method: "get",
                url: "localhost:8085/user/getUserInfo",
                params: {
                    nickName: this.nickName
                }
            }).then((res) => {
                // res:登录成功返回用户对象，登录失败需要给出对应的提示
                if (res.data.code == 200) {
                    //将用户数据存到localStorage中
                    // JSON.stringify:将json对象转换为字符串
                    localStorage.setItem("token", res.data.data.token);
                    console.log(this.nickName)
                } else {
                    alert(res.data.message)
                }
            })
        },
        // checkUserLogin() {
        //     console.log("11")
        //     const token = localStorage.getItem("token");
        //     console.log(token)
        //     if (token === null) {
        //         // 用户未登录，阻止默认导航行为
        //         // this.$router.push('');
        //         // 用户未登录，强制跳转到登录页面
        //         alert("请登录或注册！！")
        //         this.$router.push('/login');
        //     }

        // },
        logout() {
            axios({
                headers: {
                    'token': localStorage.getItem('token')
                },
                method: "get",
                url: "http://localhost:8085/user/logout"
            }).then(() => {
                localStorage.removeItem('token');
                alert('退出成功！')
                this.$router.push('/order');
            }).catch((error) => {
                // 注册失败，处理错误信息
                alert('退出失败：' + error.message);

            })
            // if (this.$route.path !== '/login') {
            //     this.$router.push('/login');
            // }

        },
        mounted() {
            // console.log(this.$route);
        }
    },
    created: function () {
        this.isLogin();
    },

}
</script>

<style scoped lang="less">

.header {
    margin-bottom: 30px;
    &>.top {
        background-color: #eaeaea;
        height: 50px;
        line-height: 50px;

        .container {
            width: 1200px;
            margin: 0 auto;
            overflow: hidden;

            .loginList {
                float: left;

                p {
                    float: left;
                    margin-right: 10px;

                    .register {
                        border-left: 1px solid #b3aeae;
                        padding: 0 5px;
                        margin-left: 5px;
                    }

                    .home {
                        border-left: 1px solid #b3aeae;
                        padding: 0 5px;
                        margin-left: 5px;
                    }
                }
            }

            .typeList {
                float: right;

                a {
                    padding: 0 10px;

                    &+a {
                        border-left: 1px solid #b3aeae;
                    }
                }
            }
        }
    }

    &>.bottom {
        width: 1200px;
        margin: 0 auto;
        overflow: hidden;

        .logoArea {
            float: left;

            .logo {
                img {
                    width: 78px;
                    margin: 25px 45px;
                }
            }
        }

        .searchArea {
            float: right;
            margin-top: 35px;

            .searchForm {
                overflow: hidden;

                input {
                    box-sizing: border-box;
                    width: 490px;
                    height: 32px;
                    padding: 0px 4px;
                    border: 2px solid #ea4a36;
                    float: left;

                    &:focus {
                        outline: none;
                    }
                }

                button {
                    height: 32px;
                    width: 68px;
                    background-color: #ea4a36;
                    border: none;
                    color: #fff;
                    float: left;
                    cursor: pointer;

                    &:focus {
                        outline: none;
                    }
                }
            }
        }
    }
}
</style>
